#!/bin/bash

echo "redis.setup"

BASEDIR=$(dirname $0)
TEMPLATE="${BASEDIR}/redis.conf"

if [[ $# -eq 0 ]]
then
	TARGET="${BASEDIR}/var"
else
	TARGET=$1
fi

if [ ! -d "${TARGET}" ]
then
	mkdir $TARGET
fi

BASEPORT=6379

for i in `seq 0 32`
do
	NAME="redis${i}"
	DIR="${TARGET}/${NAME}"
	if [ ! -d "${DIR}" ]
	then
		mkdir $DIR
	fi

	PORT=$(($i + $BASEPORT))
	PIDFILE="${NAME}.pid"
	LOGFILE="${NAME}.log"
	DBFILE="${NAME}.rdb"

	cat ${TEMPLATE} | 
		sed "s@logfile \"\"@logfile ${LOGFILE}@g" | 
		sed "s@port 6379@port ${PORT}@g" |
		sed "s@pidfile /var/run/redis.pid@pidfile ${PIDFILE}@g" |
		sed "s@dbfilename dump.rdb@dbfilename ${DBFILE}@g" > ${DIR}/${NAME}.conf
done

